package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.kubernetes.models.RoleBindingRequestData;
import com.ruoyi.kubernetes.service.IKubernetesClientRoleBindingService;
import com.ruoyi.web.mapper.KubernetesUserRoleBindingMapper;
import com.ruoyi.web.utils.KubernetesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.domain.KubernetesUserRoleBinding;
import com.ruoyi.web.service.IKubernetesUserRoleBindingService;

/**
 * K8s用户权限Service业务层处理
 *
 * @author xuchengchao
 * @date 2022-04-17
 */
@Service
public class KubernetesUserRoleBindingServiceImpl implements IKubernetesUserRoleBindingService
{
    @Autowired
    private KubernetesUserRoleBindingMapper kubernetesUserRoleBindingMapper;

    @Autowired
    private IKubernetesClientRoleBindingService kubernetesClientRoleBindingService;
    /**
     * 查询K8s用户权限
     *
     * @param id K8s用户权限主键
     * @return K8s用户权限
     */
    @Override
    public KubernetesUserRoleBinding selectKubernetesUserRoleBindingById(Long id)
    {
        return kubernetesUserRoleBindingMapper.selectKubernetesUserRoleBindingById(id);
    }

    /**
     * 查询K8s用户权限列表
     *
     * @param kubernetesUserRoleBinding K8s用户权限
     * @return K8s用户权限
     */
    @Override
    public List<KubernetesUserRoleBinding> selectKubernetesUserRoleBindingList(KubernetesUserRoleBinding kubernetesUserRoleBinding)
    {
        return kubernetesUserRoleBindingMapper.selectKubernetesUserRoleBindingList(kubernetesUserRoleBinding);
    }

    /**
     * 新增K8s用户权限
     *
     * @param kubernetesUserRoleBinding K8s用户权限
     * @return 结果
     */
    @Override
    public int insertKubernetesUserRoleBinding(KubernetesUserRoleBinding kubernetesUserRoleBinding)
    {
        RoleBindingRequestData roleBindingRequestData = KubernetesUtils.getRoleBindingRequestData(kubernetesUserRoleBinding);
        if(kubernetesClientRoleBindingService.addBinding(roleBindingRequestData)) {
            return kubernetesUserRoleBindingMapper.insertKubernetesUserRoleBinding(kubernetesUserRoleBinding);
        }else{
            return 0;
        }
    }

    /**
     * 修改K8s用户权限
     *
     * @param kubernetesUserRoleBinding K8s用户权限
     * @return 结果
     */
    @Override
    public int updateKubernetesUserRoleBinding(KubernetesUserRoleBinding kubernetesUserRoleBinding)
    {
        return kubernetesUserRoleBindingMapper.updateKubernetesUserRoleBinding(kubernetesUserRoleBinding);
    }

    /**
     * 批量删除K8s用户权限
     *
     * @param ids 需要删除的K8s用户权限主键
     * @return 结果
     */
    @Override
    public int deleteKubernetesUserRoleBindingByIds(Long[] ids)
    {
        if (ids.length==1){
            RoleBindingRequestData roleBindingRequest = KubernetesUtils.getRoleBindingRequestData(kubernetesUserRoleBindingMapper.selectKubernetesUserRoleBindingById(ids[0]));
            if (kubernetesClientRoleBindingService.deleteBinding(roleBindingRequest)) {
                return kubernetesUserRoleBindingMapper.deleteKubernetesUserRoleBindingById(ids[0]);
            }
        }
        return kubernetesUserRoleBindingMapper.deleteKubernetesUserRoleBindingByIds(ids);
    }

    /**
     * 删除K8s用户权限信息
     *
     * @param id K8s用户权限主键
     * @return 结果
     */
    @Override
    public int deleteKubernetesUserRoleBindingById(Long id)
    {
        RoleBindingRequestData roleBindingRequest = KubernetesUtils.getRoleBindingRequestData(kubernetesUserRoleBindingMapper.selectKubernetesUserRoleBindingById(id));
        if (kubernetesClientRoleBindingService.deleteBinding(roleBindingRequest)) {
            return kubernetesUserRoleBindingMapper.deleteKubernetesUserRoleBindingById(id);
        }else {
            return 0;
        }
    }
}
